﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.WPF;
using CSharp.Libs.ExceptionHandling;
using System.Collections.ObjectModel;
using CSharp.Libs.Koc.ItemClasses;
using System.IO;
using System.Reflection;
using System.Collections;

namespace CSharp.Libs.Koc.UserControls
{
    class KocServerActiveViewModel : ViewModelBase
    {
        #region declarations
        private DatabaseOperations _DBOps = null;
        private ObservableCollection<KocServerActiveItem> _ocKocServerActiveItems = new ObservableCollection<KocServerActiveItem>();
        #endregion
        


        #region constructor / finalizer
        public KocServerActiveViewModel()
        {
            _DBOps = new DatabaseOperations();
            _ocKocServerActiveItems.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_ocKocServerActiveItems_CollectionChanged);
        }
        #endregion



        #region event handling
        void _ocKocServerActiveItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                foreach (KocServerActiveItem KSAI in e.OldItems)
                {
                    _DBOps.ServerActiveDeleteItem(KSAI.ID);
                }
            }
        }
        #endregion



        #region private functions
        /// <summary>_InitDB</summary>
        /// <param name="FileName"></param>
        /// <history>created  24.11.2011</history>
        private void _InitDB(
            String FullFileName)
        {
            _DBOps.InitDb(FullFileName);

            ArrayList alServersActive = _DBOps.ServerActiveGetItems();

            if (alServersActive != null &&
                alServersActive.Count > 0)
            {
                _ocKocServerActiveItems.Clear();

                foreach (KocServerActiveItem KSAI in alServersActive)
                {
                    _ocKocServerActiveItems.Add(KSAI);
                }

                OnPropertyChanged("KocServerActiveItems");
            }
        }

        /// <summary>_SaveDB</summary>
        /// <history>created  24.11.2011</history>
        private void _SaveDB()
        {
            if (_ocKocServerActiveItems.Count > 0)
            {
                foreach(KocServerActiveItem KSAI in _ocKocServerActiveItems)
                {
                    if(_DBOps.ServerActiveServerNumberExists(KSAI.ServerNumber))
                    {
                        _DBOps.ServerActiveUpdateItem(KSAI.ID, KSAI.ServerNumber, KSAI.ServerActive);
                    }
                    else
                    {
                        _DBOps.ServerActiveCreateItem(KSAI.ServerNumber, KSAI.ServerActive);
                    }
                }
            }
        }
        #endregion
        
        
        
        #region interface
        #region properties
        public ObservableCollection<KocServerActiveItem> KocServerActiveItems
        {
            get
            {
                return _ocKocServerActiveItems;
            }
        }

        #endregion

        #region methods
        public void InitDB(
            String FullFileName)
        {
            _InitDB(FullFileName);
        }

        public void SaveDB()
        {
            _SaveDB();
        }
        #endregion
        #endregion
    }
}
